import os

# 确保环境变量被加载
from dotenv import load_dotenv

from app import create_app
from app.extensions import db

load_dotenv()

app = create_app(os.getenv('FLASK_CONFIG') or 'default')


@app.cli.command()
def init_db():
    """初始化数据库"""
    db.create_all()
    print("数据库已初始化")


@app.cli.command()
def drop_db():
    """删除数据库"""
    db.drop_all()
    print("数据库已删除")


@app.cli.command()
def seed_data():
    """添加示例数据"""
    from app.services.string_util_service import StringGenerator

    # 生成一些示例数据
    examples = [
        (10, 'digits'),
        (8, 'letters'),
        (12, 'digits_letters'),
        (6, 'symbols'),
        (5, 'chinese', '编号:', ''),
        (15, 'custom', '', '', 'ABCDEF123')
    ]

    for example in examples:
        if len(example) == 2:
            StringGenerator.generate_and_save(example[0], example[1])
        elif len(example) == 3:
            StringGenerator.generate_and_save(example[0], example[1], example[2])
        else:
            StringGenerator.generate_and_save(example[0], example[1], example[2], '', example[3])

    print("示例数据已添加")


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
